package name.nano.client;

import gwt.canvas.client.Canvas;

import java.util.ArrayList;
import java.util.Iterator;

import name.nano.client.dto.LocationInTime;
import name.nano.client.dto.PlayerTactic;
import name.nano.client.dto.TeamTactic;
import name.nano.client.utils.Coordinate;
import name.nano.client.utils.Rectangle;



public class GameBoard {
	
	private static final String COLOR_YELLOW = "#FFFF00";
	private static final String COLOR_BLUE = "#0000FF";
	private static final String COLOR_GREEN = "#00FF00";
	private static final String COLOR_BLACK = "#FFFFFF";
	ArrayList<UltimatePlayer> m_players=new ArrayList<UltimatePlayer>();
	private Coordinate m_fieldSize=null;
	private FrisbeeTacticBoard m_simulator;
	private LocationInTime m_nextWaipoint;
	TeamTactic teamTactic;
	


	public GameBoard(FrisbeeTacticBoard simulator,TeamTactic teamTactic) {
		super();
		//this.fieldSize=size;
		this.m_simulator=simulator;
		this.teamTactic=teamTactic;
		init();
	}

private void init() {
		PlayerTactic[] pt = teamTactic.getPlayersTactic();
		this.m_fieldSize=teamTactic.getFieldSize();
		int diskAt = teamTactic.getPlayerWithDisk();
		for (int i = 0; i < pt.length; i++) {
			UltimatePlayer ultimatePlayer = new UltimatePlayer(m_simulator,pt[i].getStartLocation(),pt[i].getPlayerMode(),pt[i].color);
			m_players.add(ultimatePlayer);
			if (i==diskAt){
				m_simulator.setPlayerWithDisc(ultimatePlayer);
			}
			ArrayList<LocationInTime> moves = pt[i].getMoves();
			if (moves!=null && moves.size()>0){
				for (int j = 0; j < moves.size(); j++) {
					ultimatePlayer.addTargetLocation(moves.get(j));
				}
			}
		}
		for (int i = 0; i < pt.length; i++) {
			if (pt[i].getOpponentNumber()!=null)
				m_players.get(i).setManDefenceOpponent(m_players.get(pt[i].getOpponentNumber()));
		}
		
	}

//	private void startOnTheLine(FrisbeeTacticBoard simulator) {
//		int dx = (int) (fieldSize.x/(double)9);
//		int x=dx;
//		
//		for (int i=0;i<7;i++){
//			UltimatePlayer offencePlayer = new UltimatePlayer(simulator,new Location(x,baselineOffence),GameMode.OFFENCE,"#FF0000");
//			players.add(offencePlayer);
//						
//			x=x+dx;
//		}
//		simulator.setPlayerWithDisc(players.get(3));
//		x=dx;
//		for (int i=0;i<7;i++){
//			UltimatePlayer defencePlayer = new UltimatePlayer(simulator,new Location(x,baselineDefence),GameMode.DEFENCE,"#0000FF");
//			players.add(defencePlayer);
//			defencePlayer.setManDefenceOpponent(players.get(i));
//			x=x+dx;
//		}
//	}



public boolean nextStep(long milliseconds) {
	boolean moreWorkToDo=false;
	Iterator<UltimatePlayer> i = m_players.iterator();
	while (i.hasNext()){
		UltimatePlayer p=i.next();
		boolean tmp=p.nextStep(milliseconds);
		moreWorkToDo=moreWorkToDo||tmp;
	}
	return moreWorkToDo;
}
public void resetPlayers() {
	Iterator<UltimatePlayer> i = m_players.iterator();
	while (i.hasNext()){
		UltimatePlayer p=i.next();
		p.setCurrentRunningIndex(0);
	}
}

	public void drawBoard(Canvas m_canvas) {
		m_canvas.setBackgroundColor("#A0FFA0");
		m_canvas.clear();
		
		int canvasWidth = m_canvas.getWidth();
		Rectangle drawingBounds=new Rectangle(0,0,canvasWidth,m_canvas.getHeight());
		m_canvas.setStrokeStyle(COLOR_YELLOW);
		m_canvas.strokeRect(1,1,drawingBounds.width-1,drawingBounds.height-1);
		
		
		m_canvas.setFillStyle(COLOR_GREEN);
		double defenceLinePixel = m_simulator.getSY()*teamTactic.getBaselineDefence();
		double offenceLinePixels = m_simulator.getSY()*teamTactic.getBaselineOffence();
		
		m_canvas.fillRect(0, 0, canvasWidth, defenceLinePixel);
		m_canvas.fillRect(0, offenceLinePixels, canvasWidth,m_fieldSize.y*m_simulator.getSY() );
		
		Iterator<UltimatePlayer> iterator = m_players.iterator();
		while (iterator.hasNext()) {
			UltimatePlayer p= (UltimatePlayer) iterator.next();
			Coordinate location = p.getLocation();
			
			
			double scX = toScX(location);
			double scY = toScY(location);
			double threeMeterPixel = 3*m_simulator.getSX();
			
			double playerMeterPixel = 0.5*m_simulator.getSX();
			if (p==m_simulator.getSelectedPlayer()){
				m_canvas.setFillStyle(COLOR_BLACK);
				m_canvas.fillRect(scX -2*playerMeterPixel, scY -2*playerMeterPixel , 4*playerMeterPixel, 4*playerMeterPixel);
			}
			m_canvas.setFillStyle(p.getColor());
			m_canvas.fillRect(scX -playerMeterPixel, scY -playerMeterPixel , 2*playerMeterPixel, 2*playerMeterPixel);
			if (p==m_simulator.getPlayerWithDisc()){
				m_canvas.setStrokeStyle(COLOR_BLUE);
				m_canvas.setLineWidth(1);
				m_canvas.beginPath();
				
				m_canvas.arc(scX, scY, threeMeterPixel, 0, 6.28, false);
				m_canvas.stroke();
				//m_canvas.strokeRect(scX-threeMeterPixel,scY-threeMeterPixel,2*threeMeterPixel,2*threeMeterPixel);
			}
			ArrayList<LocationInTime> points = p.getRunningPoints();
			
			if (p.getRunningPoints().size()>0){
				m_canvas.setLineWidth(1);
				
				m_canvas.beginPath();
				Coordinate startPoint = p.getLocation();
				m_canvas.moveTo(toScX(startPoint), toScY(startPoint));
				int i=p.m_currentTargetIndex;
				while (i<points.size()){
					Coordinate loc=points.get(i);
					m_canvas.lineTo(toScX(loc), toScY(loc));
					i++;
				}
				m_canvas.stroke();
				
			}
			
		}
		
	}

	private double toScY(Coordinate loc) {
		return loc.y*m_simulator.getSY();
	}

	private double toScX(Coordinate loc) {
		return loc.x*m_simulator.getSX();
	}


	public void onMouseDown(Coordinate downEv) {
		UltimatePlayer selectedPlayer = getSelectedPlayer();
		if (selectedPlayer!=null && selectedPlayer!=findPlayerAt(downEv))
			m_nextWaipoint=new LocationInTime(downEv.x,downEv.y,m_simulator.getCurrentSliderValue());
			
	}
	public void onMouseMove(Coordinate downEv) {
		//m_playerToMove=findPlayerAt(downEv);
	}

	public UltimatePlayer findPlayerAt(Coordinate downEv) {
		Iterator<UltimatePlayer> i=m_players.iterator();
		while (i.hasNext()){
			UltimatePlayer p = (UltimatePlayer) i.next();
			if (p.m_location.distance(downEv)<0.5){
				return p;
			}
			
		}
		return null;
	}

	public void onMouseUp(Coordinate downEv) {
		if (m_nextWaipoint!=null){
			if (m_nextWaipoint.distance(downEv)<0.5){
				getSelectedPlayer().addWaypoint(m_nextWaipoint);
				m_nextWaipoint=null;
				return;
			}
		}
		UltimatePlayer playerHit = findPlayerAt(downEv);
		if (playerHit!=null && playerHit!=getSelectedPlayer()){
			this.m_simulator.setSelectedPlayer(playerHit);
		} else if (playerHit==getSelectedPlayer()){
			this.m_simulator.setSelectedPlayer(null);
		}
		
	}

	private UltimatePlayer getSelectedPlayer() {
		return this.m_simulator.getSelectedPlayer();
	}

	public Coordinate getFieldSize() {
		// TODO Auto-generated method stub
		return this.m_fieldSize;
	}

	public ArrayList<UltimatePlayer> getPlayers() {
		return m_players;
		
	}

	
	
	

}
